package com.weiaibenpao.demo.chislim.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.weiaibenpao.demo.chislim.utils.Utils;

/**
 * Created by wlx on 2017/12/29.
 */

public class CirCleProgressBar extends ProgressBar {
    /**
     * 定义设置进度圆的默认半径
     */
    private int mRadius = Utils.dp2px(50);

    /**
     * 圆环的默认宽度
     */
    private  int mProgressBarHeight = Utils.dp2px(6);

    /**
     * 声明初始化一个画笔
     */
    private Paint mPaint = new Paint();
    /**
     * 设置未加载进度的默认颜色
     */
    private int mUnReachedBarColor = Color.parseColor("#E5E3E1");

    /**
     * 设置已加载进度的默认颜色
     */
    private int mReachedBarColor = Color.parseColor("#FF5218");
    private RectF mArcRectF;
    private int degree=60;

    public CirCleProgressBar(Context context) {
        this(context, null);
    }

    public CirCleProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CirCleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (context == null || attrs == null) {
            return;
        }
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(mProgressBarHeight);

    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if(widthMode!=MeasureSpec.EXACTLY){
            int widthSize = (int) (mRadius*2+mProgressBarHeight*2);
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize,MeasureSpec.EXACTLY);
        }
        if(heightMode != MeasureSpec.EXACTLY){
            int heightSize = (int) (mRadius*2+mProgressBarHeight*2);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize,MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }


        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mArcRectF = new RectF(mProgressBarHeight,
                    mProgressBarHeight,
                    mRadius*2 + mProgressBarHeight,
                    mRadius*2 + mProgressBarHeight);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.save();
        /**
         * 将画布移动到中心
         */
//        canvas.translate(getPaddingLeft()+mProgressBarHeight/2,getPaddingTop()+mProgressBarHeight/2);
//        mPaint.setStyle(Paint.Style.STROKE);
//        canvas.drawBitmap();
        float sweepAngle=getProgress()*1.0f/getMax()*360;
        int angle =degree/2;
        float targetDegree = (360-degree)*sweepAngle;
        /**
         * 绘制未加载的进度
         */
        mPaint.setColor(mUnReachedBarColor);
        canvas.drawArc(mArcRectF,0,360,false,mPaint);

//        canvas.drawArc(mArcRectF,90+angle+targetDegree,(360-degree)-targetDegree,false,mPaint);

        /**
         * 绘制已加载的圆环进度
         */
        mPaint.setColor(mReachedBarColor);
//        canvas.drawArc(mArcRectF,90+angle,targetDegree,false,mPaint);
        canvas.drawArc(mArcRectF,90,sweepAngle,false,mPaint);




        canvas.restore();
    }

    /**
     * 设置进度已加载进度条的颜色
     * @param color
     */
    public void setReachedBarColor(int color){
        this.mReachedBarColor = color;
    }





}
